import time

def getSecToNext():
    path = 'time.txt'
    life = 10
    while life>0:
        try:
            f = open(path,'r')
        except:
            life = life-1
            time.sleep(30)
        else:
            seclist = []          
            cur = list(time.localtime())
            cursec = time.mktime(cur)
            n = int(f.readline())
            for i in range(n):
                t = f.readline().strip()
                temp = t.split(':')
                cur[3]=int(temp[0].strip())
                cur[4]=int(temp[1].strip())
                nextsec = time.mktime(cur)
                if nextsec-cursec>0:
                    f.close()
                    return nextsec-cursec
                else:
                    seclist.append(nextsec)
            f.close()
            sec = min(seclist)
            return sec+24*3600-cursec
    print 'error in getSecToNext: can`t open',path
    return False